package hot;

/**
 * @author way
 * @create 2025-03-07 21:45
 */
public class _189_rotate {
    public void rotate1(int[] nums, int k) {
        int n = nums.length;
        int[] rA = new int[n];
        for (int i = 0; i < n; i++) {
            rA[(i + k) % n] = nums[i];
        }
        System.arraycopy(rA, 0, nums, 0, n);
    }

    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        reverse(nums, 0, n - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, n - 1);
    }

    private void reverse(int[] nums, int i, int j) {
        while (i < j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            i++;
            j--;
        }
    }
}
